package com.msmall.base.pager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;


public class PaginationSupport implements java.io.Serializable {

	private static final long serialVersionUID = 7030048407899258634L;

	public static final String ASC = "asc";
	public static final String DESC = "desc";
	
	// 排序字段,对应order
	protected String orderBy = null;
	
	// 排序方式
	protected String order = null;
	
	public final static int PAGESIZE = 10;

	protected int pageSize = PAGESIZE; //每页显示记录数

	protected List items = Collections.emptyList(); // 记录列表

	protected int totalCount = 0; // 共多少条记录
	
	protected boolean autoCount = true; // 是否自动查询总记录数

	protected int startIndex = 0; // 当前页的开始记录号
	
	protected int pageIndex = 1; // 当前第几页；例如pageIndex=1;pageSize=5;startIndex=5;indexes[0]=0,indexes[1]=5
	
	protected int totalPage = 0; // 共多少页

	protected Class clazz; // 当前取的类名称
	
	protected List<PropertyFilter> filters; // 条件过虑列表

	protected String queryString; // 查询语句, start from 'FROM' exp: FROM USER WHERE...
	
	protected String url;
	
	protected HttpServletRequest request;
	
	public PaginationSupport() {
	}
	
	public PaginationSupport(int pageIndex, int pageSize) {
		this.setPageIndex(pageIndex);
		this.setPageSize(pageSize);
	}
	public PaginationSupport(int pageIndex, int pageSize, boolean autocount) {
		this.setPageIndex(pageIndex);
		this.setPageSize(pageSize);
		this.setAutoCount(autoCount);
	}
	
	public void addFilters(List<PropertyFilter> list){
		if(filters == null){
			filters = new ArrayList<PropertyFilter>();
		}
		filters.addAll(list);
	}
	public void addFilter(PropertyFilter filter){
		if(filters == null){
			filters = new ArrayList<PropertyFilter>();
		}
		filters.add(filter);
	}
	public void addFilter(String name, String match, Object value){
		PropertyFilter filter = new PropertyFilter(name, match, value);
		addFilter(filter);
	}
	public void addFilter(String name, MatchType match, Object value){
		PropertyFilter filter = new PropertyFilter(name, match,value);
		addFilter(filter);
	}
	public void addFilter(String name, String match, HttpServletRequest req){
		String value = req.getParameter(name);
		if(null == value || "".equals(value))
			return;
		addFilter(name, match, value);
	}
	public void addFilter(String name, MatchType match, HttpServletRequest req){
		String value = req.getParameter(name);
		if(null == value || "".equals(value))
			return;
		addFilter(name, match, value);
	}
	
	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if(pageSize <= 0)
			pageSize = PAGESIZE;
		else
			this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		int totalPage = 0;
		if (totalCount > 0) {
			this.totalCount = totalCount;
			totalPage = totalCount / pageSize;
			if (totalCount % pageSize > 0)
				totalPage++;
		} else {
			this.totalCount = 0;
		}
		this.setTotalPage(totalPage);
	}
	/**
	 * 查询对象时是否自动另外执行count查询获取总记录数, 默认为true.
	 */
	public boolean isAutoCount() {
		return autoCount;
	}

	/**
	 * 查询对象时是否自动另外执行count查询获取总记录数.
	 */
	public void setAutoCount(final boolean autoCount) {
		this.autoCount = autoCount;
	}
	
	public int getStartIndex() {
		/*
		if(pageIndex < 0){
			return 0;
		}else{
			return pageIndex*pageSize;
		}
		*/
		if(pageIndex < 1){
			return 0;
		}else{
			return (pageIndex - 1)*pageSize;
		}
	}

	public void setStartIndex(int startIndex) {
		if (startIndex < 0)
			this.startIndex = 0;
		else
			this.startIndex = startIndex;
	}

	public Class getClazz() {
		return clazz;
	}

	public void setClazz(Class clazz) {
		this.clazz = clazz;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		if(pageIndex < 1){
			pageIndex = 1;
		}else{
			this.pageIndex = pageIndex;
		}
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public String getUrl() {
		if(url != null && !"".equals(url)){
			if(url.indexOf("?") == -1){
				url.concat("?1=1");
			}
		}
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public List<PropertyFilter> getFilters() {
		return filters;
	}

	public void setFilters(List<PropertyFilter> filters) {
		this.filters = filters;
	}
	
	/**
	 * 是否已设置排序字段,无默认值.
	 */
	public boolean isOrderBySetted() {
		return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
	}

	/**
	 * 获得排序字段,无默认值.多个排序字段时用','分隔.
	 */
	public String getOrderBy() {
		return orderBy;
	}
	/**
	 * 设置排序字段,多个排序字段时用','分隔.
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getOrder() {
		return order;
	}
	/**
	 * 设置排序方式向.
	 * 
	 * @param order 可选值为desc或asc,多个排序字段时用','分隔.
	 */
	public void setOrder(String order) {
		//检查order字符串的合法值
		String[] orders = StringUtils.split(StringUtils.lowerCase(order), ',');
		for (String orderStr : orders) {
			if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr))
				throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
		}

		this.order = StringUtils.lowerCase(order);
	}
	
	public void initPageString(ServletRequest request) {}
	public void updateParams(String name, Object value){}
	public void refreshUpdateParams(){}
}
